package com.alibaba.otter.manager.web.dto;

import com.alibaba.otter.canal.instance.manager.model.CanalParameter;
import com.alibaba.otter.canal.instance.manager.model.CanalStatus;
import com.alibaba.otter.shared.common.model.config.pipeline.Pipeline;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.Instant;
import java.util.List;

@Setter
@Getter
@NoArgsConstructor
public class CanalDTO {

    private Long id;

    private String name;
    // 对应的名字
    private String desc;
    // 描述
    private CanalStatus status;

    private CanalParameterDTO canalParameter;
    // 参数定义
    private Instant gmtCreate;
    // 创建时间
    private Instant gmtModified;

    private boolean used;

    private List<Pipeline> pipelines;


    public String getUrl() {
        CanalParameter parameter = getCanalParameter();
        if (parameter.getHaMode().isMedia()) {
            return "media://" + parameter.getMediaGroup();
        } else {
            StringBuilder address = new StringBuilder("jdbc://");
            for (List<CanalParameter.DataSourcing> groupAddress : parameter.getGroupDbAddresses()) {
                int i = 0;
                for (CanalParameter.DataSourcing dbAddress : groupAddress) {
                    ++i;
                    address.append(dbAddress.getDbAddress().getHostString())
                            .append(":")
                            .append(dbAddress.getDbAddress().getPort());

                    if (i < groupAddress.size()) {
                        address.append(',');
                    }
                }

                address.append(';');
            }

            return address.toString();
        }
    }
}
